package org.jeecg.modules.common.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.common.entity.CamelliaDevice;
import org.jeecg.modules.common.entity.FluxStatistical;
import org.jeecg.modules.common.entity.FluxStatisticalPower;
import org.jeecg.modules.common.service.ICamelliaInFluxStatisticalService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Classname CamelliaInfluxStatisticalController
 * @Description TODO
 * @Date 2023/1/5 16:03
 * @Created by 10835
 */
@Api(tags="InFluxDB 统计数据查询")
@RestController
@RequestMapping("/common/camelliaStatistical")
public class CamelliaInfluxStatisticalController {
    @Resource
    ICamelliaInFluxStatisticalService camelliaInFluxStatisticalService;


    @ApiOperation(value="InFluxDBg功耗数据-统计数据查询", notes="InFluxDB统计数据-统计数据查询")
    @GetMapping(value = "/power")
    public Result<FluxStatisticalPower> getFluxStatistical() {
        String rawFluxString = "from(bucket: \"camellia-data-analysis\")\n" +
                "  |> range(start: -7d)\n" +
                "  |> filter(fn: (r) => r[\"_measurement\"] == \"camellia_device_status\")\n" +
                "  |> filter(fn: (r) => r[\"_field\"] == \"power_sum\")";
        String fluxString = String.format(rawFluxString);
            FluxStatisticalPower fluxStatisticalPower = camelliaInFluxStatisticalService.getFluxStatisticalPower(fluxString);
        return Result.OK(fluxStatisticalPower);
    }


    /**
     * 分页列表查询
     * @param
     * @return
     */
    @ApiOperation(value="InFluxDB统计数据-统计数据查询", notes="InFluxDB统计数据-统计数据查询")
    @GetMapping(value = "/statistical")
    public Result<FluxStatistical> getFluxStatistical(@RequestParam(name="measurement") String measurement,@RequestParam(name="index") String index,@RequestParam(name="pid") String pid) {
        String rawFluxString = "from(bucket: \"camellia-data-analysis\")\n" +
                "  |> range(start: -1d)\n" +
                "  |> filter(fn: (r) => r[\"_measurement\"] == \"%s\")\n" +
                "  |> filter(fn: (r) => r[\"_field\"] == \"max_%s\")\n" +
                "  |> filter(fn: (r) => r[\"clientId\"] == \"%s\")\n" +
                "  |> max()\n" +
                "  |> yield(name: \"max\")\n" +
                "\n" +
                "\n" +
                "\n" +
                "from(bucket: \"camellia-data-analysis\")\n" +
                "  |> range(start: -1d)\n" +
                "  |> filter(fn: (r) => r[\"_measurement\"] == \"%s\")\n" +
                "  |> filter(fn: (r) => r[\"_field\"] == \"min_%s\")\n" +
                "  |> filter(fn: (r) => r[\"clientId\"] == \"%s\")\n" +
                "  |> min()  \n" +
                "  |> yield(name: \"min\")\n" +
                "\n" +
                "from(bucket: \"camellia-data-analysis\")\n" +
                "  |> range(start: -1d)\n" +
                "  |> filter(fn: (r) => r[\"_measurement\"] == \"%s\")\n" +
                "  |> filter(fn: (r) => r[\"_field\"] == \"mean_%s\")\n" +
                "  |> filter(fn: (r) => r[\"clientId\"] == \"%s\")\n" +
                "  |> mean()  \n" +
                "  |> yield(name: \"mean\")\n" +
                "\n" +
                "from(bucket: \"camellia-data-analysis\")\n" +
                "  |> range(start: -1d)\n" +
                "  |> filter(fn: (r) => r[\"_measurement\"] == \"%s\")\n" +
                "  |> filter(fn: (r) => r[\"_field\"] == \"median_%s\")\n" +
                "  |> filter(fn: (r) => r[\"clientId\"] == \"%s\")\n" +
                "  |> median()  \n" +
                "  |> yield(name: \"median\")  ";
        String fluxString = String.format(rawFluxString, measurement,index,pid,measurement,index,pid,measurement,index,pid,measurement,index,pid);
        FluxStatistical fluxStatistical = camelliaInFluxStatisticalService.getFluxStatistical(fluxString);
        return Result.OK(fluxStatistical);
    }

}
